package com.qianye.wenxin.impl;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.qianye.wenxin.WenXinApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * @Author: hjz
 * @DataTime: 2024/9/15 15:12
 **/
@Service
public class WenXinApiImpl implements WenXinApi {
    @Value("${wenxin.accessKey}")
    private String accessKey;
    @Value("${wenxin.secretKey}")
    private String secretKey;

    private Qianfan qianfan;

    @PostConstruct
    public void init() {
        this.qianfan = new Qianfan("OAuth", accessKey, secretKey);
    }

    @Override
    public String getAIChatResult(String prompt, String question) {
        return chatWithAI(prompt + "回答如下问题:" + question);
    }

    @Override
    public String getAIChatResult(String question) {
        return chatWithAI(question);
    }

    private String chatWithAI(String message) {
        ChatResponse response = qianfan.chatCompletion()
                .model("ERNIE-4.0-8K")
                .addMessage("user", message)
                .temperature(0.7)
                .execute();

        return response.getResult();
    }
}
